"""获取豆瓣电影排行榜的电影信息，保存在csv文件中"""
import csv
import re

import requests

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"}
html = requests.get(
    "https://movie.douban.com/j/chart/top_list?type=24&interval_id=100%3A90&action=&start=0&limit=20",
    headers=header).text
# print(html)
re0 = re.compile('"regions":\["(?P<area>.*?)"'
                 '.*?"title":"(?P<name>.*?)"'
                 '.*?release_date":"(?P<year>\d+)'
                 '.*?"score":"(?P<score>.*?)"')
iter1 = re0.finditer(html)
with open("data.csv", mode="w", encoding="utf-8", newline="") as f:
    csv0 = csv.writer(f)
    for i in iter1:
        print(i.group('name'))
        print(i.group('area'))
        print(i.group('year'))
        print(i.group('score'))

        dict1 = i.groupdict()
        csv0.writerow(dict1.values())
